{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2b1d73ea-645e-43f8-82ec-4b5b95e9b7bf",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install tensorflow"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "80230464-b8d8-4824-a13c-0d8b34497997",
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "imdb=tf.keras.datasets.imdb\n",
    "(x_train,y_train),(x_test,y_test)=imdb_data(num_words=4000)\n",
    "print(\"x_train.shape=\",x_train.shape)\n",
    "print(\"y_train.shape=\",y_train.shape)\n",
    "print(\"x_test.shape=\",x_test.shape)\n",
    "print(\"y_test.shape=\",y_test.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a9a5d727-f45d-4175-a488-6b9634d71146",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"序列填充前的第一个元素:\\n\",x_train[0])\n",
    "x_train=tf.keras.preprocessing.sequence.pad_sequences(x_train,padding='post',maxlen=400,truncating='post')\n",
    "x_test=tf.keras.preprocessing.sequence.pad_sequences(x_test,padding='post',maxlen=400,truncating='post')\n",
    "print(\"序列填充后的第一个元素:\\n\",x_train[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "91e45f7f-7e2a-4c6a-af2b-1b0095e44c44",
   "metadata": {},
   "outputs": [],
   "source": [
    "model=tf.keras.models.Sequential()\n",
    "model.add(tf.keras.layers.Embedding(output_dim=32,input_dim=4000,input_length))\n",
    "model.add(tf.keras.layers.Dropout(0.3))\n",
    "model.add(tf.keras.layers.GRU(64))\n",
    "model.add(tf.keras.layers.Dropout(0.3))\n",
    "model.add(tf.keras.lyers.Dense(1,activation='sigmoid'))\n",
    "model.summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "64a87566-f0f7-4ad3-864c-e89cd972e576",
   "metadata": {},
   "outputs": [],
   "source": [
    "model.compile(optimizer='rmsprop',loss='binaray_crossentropy',metrics=['accuracy'])\n",
    "history=model.fit(x_train,y_train,batch_size=64,epochs=10,validation_split=0.2)\n",
    "model.evaluate(x_test,y_test,batch_size=64,verbose=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "101497e6-1117-442d-83f8-6d746013133b",
   "metadata": {},
   "outputs": [],
   "source": [
    "loss=history.history['loss']\n",
    "acc=history.history['accuracy']\n",
    "val_loss=history.history['val_loss']\n",
    "val_acc=history.history['val_accuracy']\n",
    "plt.figure(figsize=(10,3))\n",
    "plt.subplot(121)\n",
    "plt.plot(loss,color='b',label='train')\n",
    "plt.plot(val_loss,color='r',label='validate')\n",
    "plt.ylabel('loss')\n",
    "plt.legend()\n",
    "plt.subplot(122)\n",
    "plt.plot(acc,color='b',label='train')\n",
    "plt.plot(val_acc,color='r',label='validate')\n",
    "plt.ylabel('Accuracy')\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
